package com.aitou.mrylive.domain;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.tencent.bugly.crashreport.CrashReport;
import com.aitou.mrylive.data.bean.CameraSize;
import com.aitou.mrylive.data.bean.LoginInfo;
import com.aitou.mrylive.data.bean.local.PayChannel;
import com.aitou.mrylive.data.sharedpreference.PrefsHelper;
import com.aitou.mrylive.data.websocket.WsObjectPool;

/**
 * 管理本地数据的存储，抽象解耦非简单配置项的本地数据存储位置（File、DB、SP），例如用户信息。
 * @author Muyangmin
 * @since 1.0.0
 */
public class LocalDataManager {

    private static LocalDataManager instance;

    private LocalDataManager() {

    }

    public static LocalDataManager getInstance() {
        if (instance == null) {
            synchronized (LocalDataManager.class) {
                if (instance == null) {
                    instance = new LocalDataManager();
                }
            }
        }
        return instance;
    }

    private LoginInfo mLoginInfo;

    public void saveLoginInfo(@NonNull LoginInfo loginInfo) {
        //Update cached object!
        mLoginInfo = loginInfo;
        PrefsHelper.setLoginInfo(loginInfo);

        WsObjectPool.init(loginInfo);

        CrashReport.setUserId(loginInfo.getUserId());
    }

    public void clearLoginInfo(){
        mLoginInfo = null;
        PrefsHelper.removeLoginInfo();
        WsObjectPool.release();
    }

    public LoginInfo getLoginInfo() {
        if (mLoginInfo == null) {
            mLoginInfo = PrefsHelper.getLoginInfo();
        }
        return mLoginInfo;
    }

    public void saveCameraSize(@NonNull CameraSize size, boolean isFrontCamera){
        PrefsHelper.saveCameraSize(size, isFrontCamera);
    }

    @Nullable
    public CameraSize getCameraSize(boolean isFrontCamera){
        return PrefsHelper.getCameraSize(isFrontCamera);
    }

    @PayChannel
    public int getPreferredPayChannel(int defValue){
        return PrefsHelper.getPreferredChannel(defValue);
    }

    public void savePreferredPayChannel(@PayChannel int channel){
        PrefsHelper.savePreferredPayChannel(channel);
    }

}
